package com.zxl.live.screen.call.helper;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

/**
 * Created by Administrator on 2017/12/11.
 */

public class CallFlashWindowHelper {

    private boolean isShowed;

    private Context context;

    private WindowManager manager;

    private WindowManager.LayoutParams layoutParams;

    public CallFlashWindowHelper(Context context) {
        this.context = context;
        manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams();
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.x = 0;
        layoutParams.y = 0;
        layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
        refreshLayoutType();
    }

    public void refreshLayoutType() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context)) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        }
    }

    public void showView(View view) {
        try {
            if (isShowed) {
                return;
            }
            manager.addView(view, layoutParams);
            isShowed = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismissView(View view) {
        try {
            manager.removeView(view);
            isShowed = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
